<script>
    layui.use(['layer', 'table', 'form'], function () {
        var table = layui.table;
        var form = layui.form;

        var dataTable = table.render({
            elem: '#dataTable'
            , height: 500
            , url: "{{ route('admin.system.user.data') }}"
            , page: true
            , cols: [[
                {field: 'id', title: 'ID', width: 80, templet: '#auto'}
                , {field: 'username', title: '用户名'}
                , {field: 'role', title: '角色'}
                , {field: 'name', title: '姓名'}
                , {field: 'phone', title: '手机'}
                , {field: 'email', title: '邮箱'}
                , {field: 'logined_at', title: '最后登录时间'}
                , {field: 'last_ip', title: '最后登录ip'}
                , {field: 'status', title: '状态', width: 110, templet: '#statustpl'}
                , {fixed: 'right', title: '操作', width: 110, align: 'center', toolbar: '#options'}
            ]]
        });

        $('#search').click(function () {
            var username = $('#username').val();
            var name = $('#name').val();
            var phone = $('#phone').val();
            var email = $('#email').val();
            var role = $('#role').val();
            var status = $('#status').val();
            dataTable.reload({
                where: {
                    username: username,
                    name: name,
                    phone: phone,
                    email: email,
                    role: role,
                    status: status
                },
                page: {curr: 1}
            });
        });

        $('#refresh').click(function () {
            var username = $('#username').val();
            var name = $('#name').val();
            var phone = $('#phone').val();
            var email = $('#email').val();
            var role = $('#role').val();
            var status = $('#status').val();
            dataTable.reload({
                where: {
                    username: username,
                    name: name,
                    phone: phone,
                    email: email,
                    role: role,
                    status: status
                },
                page: {curr: 1}
            });
        });

        form.on('checkbox(lock)', function (obj) {
            var id = this.value;
            $.post("{{ route('admin.system.user.status.set') }}", {id: id}, function (result) {
                if (result.code === '0') {
                    dataTable.reload();
                }
                layer.msg(result.msg);
            });
        });

        table.on('tool(dataTable)', function (obj) {
            var data = obj.data;
            var event = obj.event;
            if (event === 'edit') {
                location.href = '/admin/system/user/editForm/' + data.id;
            }
        });
    });
</script>